# -*- codeing = utf-8 -*-
# @Time : 2021/11/20 1:29
# @Author : ning
# @File : demo.main.py
# @software : PyCharm

import requests
import re  # 通过 re 模块解析，获取数据，利用正则表达式,进行文字匹配。
import time
import os


# 模拟人向网站发送请求
def request_get(url, ret_type="text", timeout=5, encoding="gbk"):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
    }
    respone = requests.get(url=url, headers=headers, timeout=timeout)
    respone.encoding = encoding
    if ret_type == "text":
        return respone.text
    elif ret_type == "image":
        return respone.content


# 获取网站地址的数据
def main():
    urls = [f"http://www.netbian.com/mei/index_{i}.htm" for i in range(1, 2)]
    url = "http://www.netbian.com/mei/index.htm"
    urls.insert(0, url)
    for url in urls:
        print("抓取图片的地址为：", url)
        text = request_get(url)
        format(text)


# re模块解析函数
def format(text):
    origin_text = split_str(text, '<div class="list">', '<div class="page">')
    pattern = re.compile('href="(.*?)"')
    hrefs = pattern.findall(origin_text)
    hrefs = [i for i in hrefs if i.find("desk") > 0]
    for href in hrefs:
        url = f"http://www.netbian.com{href}"
        print(f"图片正在下载：{url}")
        text = request_get(url)
        format_detail(text)


def split_str(text, s_html, e_html):
    start = text.find(s_html) + len(e_html)
    end = text.find(e_html)
    origin_text = text[start:end]
    return origin_text


def format_detail(text):
    origin_text = split_str(text, '<div class="pic">', '<div class="pic-down">')
    pattern = re.compile('src="(.*?)"')
    image_src = pattern.search(origin_text).group(1)
    # 保存图片
    save_image(image_src)


# 将图片下载并存储
def save_image(image_src):
    content = request_get(image_src, "image")  # request_get()方法获取图片
    # open 读写文件
    print("开始写文件")
    #  f字符串前，执行python
    with open(f"{str(time.time())}.jpg", mode="wb") as f:
        f.write(content)  # f.write 存储图片
        print('图片下载成功！！！')


# 调用主程序
if __name__ == "__main__":
    main()